package com.qf.controll;

import com.alibaba.fastjson.JSONObject;
import com.qf.pojo.Shop;
import com.qf.service.ShopService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/ShopServlet")
public class ShopServlet extends HttpServlet {
        //声明shopService,只要类一加载,就进行Spring bean管理
    private ShopService shopService;
    //提供无参构造方法
    public ShopServlet(){
        //获取Spring容器对象
        ClassPathXmlApplicationContext classPathXmlApplicationContext =new
                ClassPathXmlApplicationContext("spring-config.xml");
        //获取业务接口类对象
        shopService = (ShopService) classPathXmlApplicationContext.getBean("shopService");
    //打印一下地址
        System.out.println(shopService);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理中文相应乱码
        response.setContentType("text/html;charset=utf-8");
        //调用方法
        List<Shop> list = shopService.findAllShop();
        //转换成json
        String jsonString = JSONObject.toJSONString(list);
        System.out.println(jsonString);
        //相应给浏览器
        response.getWriter().write(jsonString);

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
